package cn.initcap.algorithm.swing.threegatesproblem;

import java.util.Random;

/**
 * @author initcap
 * @date 7/30/19 8:43 PM
 */
public class ThreeGatesExperiment {

    private int N;

    public ThreeGatesExperiment(int N) {

        if (N <= 0) {
            throw new IllegalArgumentException("N must be larger than 0!");
        }
        this.N = N;
    }

    public static void main(String[] args) {

        int N = 10000000;
        ThreeGatesExperiment exp = new ThreeGatesExperiment(N);

        exp.run(true);
        System.out.println();
        exp.run(false);
    }

    public void run(boolean changeDoor) {

        int wins = 0;
        for (int i = 0; i < N; i++) {
            if (play(changeDoor)) {
                wins++;
            }
        }

        System.out.println(changeDoor ? "Change" : "Not Change");
        System.out.println("winning rate: " + (double) wins / N);
    }

    private boolean play(boolean changeDoor) {

        // Door 0, 1, 2
        int prizeDoor = new Random().nextInt(3);
        int playerChoice = new Random().nextInt(3);

        if (playerChoice == prizeDoor) {
            return changeDoor ? false : true;
        } else {
            return changeDoor ? true : false;
        }
    }
}
